package hn.sigit.dao.hnd.administrative;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.Component;

import hn.sigit.model.hnd.administrative.HND_ActivityType;
import hn.sigit.model.hnd.administrative.HND_Transaction;
import hn.sigit.dao.SigitDAO;

public class HND_TransactionDAO extends SigitDAO<HND_Transaction> {
	private static final long serialVersionUID = 1L;

	public static HND_Transaction loadRequestByPresentationId(long presentationId) {
		Session commonsDatabase = (Session)Component.getInstance(COMMONS_DB);
		return loadRequestByPresentationId(commonsDatabase, presentationId);
	}
	public static HND_Transaction loadNextRequest() {
		return loadNextRequest(getSession());
	}
	public static List<HND_Transaction> loadRequestsByCurrentActivity(HND_ActivityType... activityTypes) {
		return loadRequestsByCurrentActivity(getSession(), activityTypes);
	}


	public static HND_Transaction loadRequestByPresentationId(Session session, long presentationId) {
		Criteria criteria = session.createCriteria(HND_Transaction.class);
		criteria.add(Restrictions.eq("presentationId", presentationId));
		
		return (HND_Transaction) criteria.uniqueResult();
	}
	public static HND_Transaction loadNextRequest(Session session) {
		Date oldestDate = (Date) session.createCriteria(HND_Transaction.class)
		.setProjection(Projections.min("presentationDate"))
		.add(Restrictions.eq("currentActivity", HND_ActivityType.DELIVER_FOR_EXTERNAL_APPROVAL))
		.uniqueResult();
		
		Criteria criteria = session.createCriteria(HND_Transaction.class);
		criteria.add(Restrictions.eq("presentationDate", oldestDate));
		criteria.add(Restrictions.eq("currentActivity", HND_ActivityType.DELIVER_FOR_EXTERNAL_APPROVAL));
		
		return (HND_Transaction) criteria.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public static List<HND_Transaction> loadRequestsByCurrentActivity(Session session, HND_ActivityType... activityTypes) {
		Criteria criteria = session.createCriteria(HND_Transaction.class);
		criteria.add(Restrictions.in("currentActivity", activityTypes));
		
		return criteria.list();
	}


}
